def reverse(x):
    """
    :type x: int
    :rtype: int
    """
    s = str(x) if x >= 0 else str(abs(x))
    length = len(s)
    res = 0
    for i in range(length):
        res += int(s[i]) * 10 ** (i)
    res = res if x >= 0 else res * -1
    res = 0 if res >= 2 ** 31 or res <= 2 ** 31 * -1 else res

    return res


x = -123
print(reverse(x))  # -321
